
function init_tooltip()
{
	$(".qtip").qtip({
		content : "Loading...",
		show : { delay : 200 },
		position : {
			corner : {
				target : "topMiddle",
				tooltip : "bottomLeft"
			}
		},
		style : { 
			name : "dark",
			border : {
				width : 1,
				radius : 1,
				color : "#000"
			},
			tip : {
				corner : "bottomLeft",
				color : "black"
			}
		}
	});
}

function get_score(ref_id, id1, id2)
{
	var obj = $("#t"+ref_id);
	
	if (obj.data("msg"))
		return;
		
	if (obj.data("out") == null)
	{
		obj.data("request", function()
		{
			$.ajax({
				type: "POST",
				url: "ajax.php",
				data: {
					"func" : "get_score", 
					"id1" : id1, 
					"id2" : id2},
				success: function(msg)
				{
					msg = "<b>" + msg + "</b>";
					obj.data("msg", msg);
					obj.qtip("api").updateContent(msg);
				}
			});
		});
		obj.data("out", function() 
		{
			clearTimeout(obj.data("t"));
		});
		obj.mouseout(obj.data("out"));
	}
	obj.data("t", setTimeout(obj.data("request"), 200));
}

function get_params(params){
	return params.join("&");	
}

function Chr(AsciiNum)
{
	return String.fromCharCode(AsciiNum);
}

$.fn.playerSearch = function(callback){
	return this.each(function(){
		var self = this;
		if (callback)
		{
			self.callback = callback;
		}
		else 
		{
			self.callback = function() {};
		}
		var j = $(self);
		j.val("");
		self.t = 0;
		self.div = $("<div />");
		$("body").append(self.div);
		self.div.css({
			"position" : "absolute",
			"background-color" : "#fff",
			"border" : "1px solid black",
			"left" : j.offset().left + "px",
			"top" : j.offset().top + j.height() + 5 + "px",
			"width" : j.width() + "px",
			"height" : "10px",
			"text-align" : "left",
			"padding-top" : "2px",
			"padding-left" : "2px",
			"overflow" : "hidden",
			"display" : "none"
		});
		
		j.blur(function(){
			clearTimeout(self.t);
			setTimeout(function(){
				self.div.css({"display" : "none"});				
			}, 100);
			if (j.val().match(/^\d+$/)){
				self.request();
			}
				
		});
		
		j.focus(function(){
			if (self.div.html() != "")
				self.div.css({"display" : "block"});
		});
		
		
		var process = function(event)
		{
			self.resp = null;
			if (self.t){
				clearTimeout(self.t);
			}
			if (event.which >= 48 && event.which <= 57){
				return;
			}
			if (event.which == 13)
				self.request();
			else
				self.t = setTimeout(self.request, 500);
		};
		
		self.store = function(resp)
		{
			j.val(resp.surname + ", " + resp.name);
			j.data("resp", resp);
			self.callback(resp);
		};
		
		self.request = function()
		{
			self.t = 0;
			val = $(self).val();
			if (val.length > 1)
			{
				$.ajax({
					type: "POST",
					url: "ajax.php",
					data: {
						"func" : "get_search", 
						"query" : val },
					success: function(msg)
					{
						if (msg == "")
							return;
						self.div.css("display", "none").html("");
						var resp = jQuery.parseJSON(msg);
						if (resp.length == 0)
							return;
						if (resp.length == 1){
							self.store(resp[0]);
							return;
						}
						var text = "";
						for (i = 0; i < resp.length; i++)
						{
							var text = resp[i].surname + ",&nbsp;" + resp[i].name;
							$("<div />").html(text)
							.addClass("dropdown_element")
							.click(function(){
								var elem = $(this);
								self.store(elem.data("resp"));
								self.div.css("display", "none").html("");
							})
							.data("resp", resp[i])
							.appendTo(self.div);
						}
						self.div.css({
							"height" : "auto",
							"display" : "block"
						});
					}
				});
			}
			else
			{
				self.div.css("display", "none");
			}
		};
		
		return $(this).keydown(process);
	});
};

function message(text, title, callback)
{
	if (!title)
		title = "System Message";
	if (!callback)
		callback = function(){};
	
	text = '<div class="message_content_outer"><div class="message_content_middle"><div class="message_content_inner">' 
		+ text + '</div></div>';
	
	$("#dialog")
		.html(text)
		.dialog({
			"title" : title,
			dialogClass : "alert",
			modal : true,
			resizable : false,
			close : callback
		});
};
